define(function(require, exports){
	var $ = require('jquery');
	var pubjs = require('pubjs');
	var view  = require('@base/view');

	// 落地页上传子模块
	var SubWhisky = view.container.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'fileId': 0,
				'data': null,
				'class': 'M-uploaderWhisky',
				'defaultImage': window.ROOT('resources/images/default.png')
			});

			this.$data = null;

			this.$fileId = config.get('fileId');

			this.Super('init', arguments);
		},
		afterBuild: function(){
			var c = this.getConfig();
			var el = this.getDOM();
			$([
				'<li class="" data-id=' + c.fileId +'>',
					'<div class="M-uploaderWhiskyInfo">',
						'<div class="fl">',
							'<div class="mb5"><label>'+LANG('名称：')+'</label><span class="M-uploaderWhiskyName" /></div>',
							'<div class="mb5">',
								'<label>'+LANG('类型：')+'</label><span class="M-uploaderWhiskyType" />',
								'<label class="ml20">'+LANG('大小：')+'</label><span class="M-uploaderWhiskySize" />',
							'</div>',
						'</div>',
						'<div class="fr status" />',
					'</div>',

					'<div class="M-uploaderWhiskyThumb cl"><img alt="'+LANG('缩略图')+'" src="'+c.defaultImage+'" />',
				'</li>'
			].join('')).appendTo(el);

			// 控制状态模块
			this.createAsync('assist', 'modules/upload.assist', {
				hasDelete: true,
				hasPreview: false,
				target: el.find('.status')
			});

			this.$doms = {
				type: el.find('.M-uploaderWhiskyType'),
				size: el.find('.M-uploaderWhiskySize'),
				name: el.find('.M-uploaderWhiskyName'),
				thumb: el.find('.M-uploaderWhiskyThumb img')
			};

			if(c.data){
				this.setData(c.data, true);
			}
		},
		getData: function(){
			return this.$data;
		},
		onUploadProgress: function(ev){
			var file = ev.param;
			if(this.$fileId == file.id){
				this.$.assist.update(file.percent);
			}
		},
		onFileUploaded: function(ev){
			var file = ev.param[0];
			var data = ev.param[1];

			if(this.$fileId == file.id){
				this.$.assist.update(true);
				this.setData(data);
				this.fire('uploadSuccess', data.fileId);
			}
		},
		onUploadError: function(ev){
			var file = ev.param.file;
			if(this.$fileId == file.id){
				this.$.assist.update(false);
			}
		},
		setData: function(data, isEdit){

			if(isEdit){
				this.$.assist.update(true);
			}
			var id = data.id;
			data.id = isEdit ? id : 0;
			data.fileId = isEdit ? 0 : id;

			this.$data = data;

			var doms = this.$doms;
			doms.thumb.attr('src', data.thumb+'&w=600&h=300');
			doms.name.text(data.name);
			doms.type.text(data.type);
			doms.size.text(data.size);
		},
		// 销毁模块
		onChildDeleted: function(ev){
			this.fire('childDeleted', null, 'afterChildDeleted', this);
			return false;
		},
		afterChildDeleted: function(){
			this.destroy();
		},
		getValue: function(){
			// 如果是上传，发{whiskyId: 0, fileId: 123};
			// 如果是编辑，发{whiskyId: 123, fileId: 0}
			return {
				id: this.$data.id,
				fileId: this.$data.fileId
			};
		}
	});
	exports.base = SubWhisky;
});